﻿using System;
using System.Windows.Forms;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.WindowsForms;

namespace AccelerateSensor.Client.Controls
{
    public partial class CommonChartControl : UserControl
    {
        private readonly PlotView _plotView;
        private readonly PlotModel _plotModel;
        private readonly int _sleepTime;

        public CommonChartControl(string title, int sleepTime)
        {
            InitializeComponent();

            _sleepTime = sleepTime;
            _plotView = new PlotView() {Dock = DockStyle.Fill};

            //定义model
            _plotModel = new PlotModel()
            {
                Title = title
            };

            Controls.Add(_plotView);
            InitPlotView();
        }

        private void CommonChartControl_Load(object sender, EventArgs e)
        {
        }

        #region InitPlotView

        private void InitPlotView()
        {
            AddAxes(); //添加坐标轴
            AddSeries(); //添加曲线
            _plotView.Model = _plotModel;
        }

        private void AddAxes()
        {
            //X轴
            var dateAxis = new DateTimeAxis()
            {
                MajorGridlineStyle = LineStyle.Solid,
                MinorGridlineStyle = LineStyle.Dot,
                IntervalLength = 80,
                IsZoomEnabled = false,
                IsPanEnabled = false,
                IntervalType = DateTimeIntervalType.Manual,
                StringFormat = "yyyy-MM-dd HH:mm:ss.ff"
            };
            _plotModel.Axes.Add(dateAxis);

            //Y轴
            var valueAxis = new LinearAxis()
            {
                MajorGridlineStyle = LineStyle.Solid,
                MinorGridlineStyle = LineStyle.Dot,
                IntervalLength = 80,
                Angle = 60,
                IsZoomEnabled = false,
                IsPanEnabled = false
            };
            _plotModel.Axes.Add(valueAxis);
        }

        private void AddSeries()
        {
            var series = new LineSeries()
            {
                Color = OxyColors.Blue,
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = OxyColors.BlueViolet,
                MarkerType = MarkerType.None
            };
            _plotModel.Series.Add(series);
        }

        #endregion

        public void AddData(float[] dataArray, DateTime updateTime)
        {
            var lineSeries = _plotView.Model.Series[0] as LineSeries;
            if (lineSeries != null)
            {
                lineSeries.Points.Clear();

                var index = 0;
                var min = float.MaxValue;
                var max = float.MinValue;
                var interval = _sleepTime * 1000 * 1000 * 10 / dataArray.Length;
                var start = updateTime.AddSeconds(-_sleepTime);
                foreach (var data in dataArray)
                {
                    min = min < data ? min : data;
                    max = max > data ? max : data;
                    var time = start.AddTicks(interval * index++);
                    lineSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time), data));
                }

                if (_plotModel.Axes[1] != null)
                {
                    var offset = (max - min) * 0.2f;
                    _plotModel.Axes[1].Minimum = min - offset;
                    _plotModel.Axes[1].Maximum = max + offset;
                }
            }

            _plotModel.InvalidatePlot(true);
        }
    }
}
